/*
 * UserNet  --  TCP/IP implementation in userspace
 * Copyright (C) 2010  Robert Millan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef UNET_KERNEL_H
#define UNET_KERNEL_H 1

/* Kernel calls (used as fallback by wrap.c).  */

#include <sys/socket.h>
#include <sys/syscall.h>
#include <linux/net.h>		/* socketcall */

static inline int
real_socket (int namespace, int style, int protocol)
{
#ifdef SYS_socket
  return syscall (SYS_socket, namespace, style, protocol);
#else
  unsigned long args[] = { namespace, style, protocol };
  return syscall (SYS_socketcall, SYS_SOCKET, args);
#endif
}

static inline int
real_setsockopt (int fd, int level, int optname, const void *optval, socklen_t optlen)
{
#ifdef SYS_setsockopt
  return syscall (SYS_setsockopt, fd, level, optname, optval, optlen);
#else
  unsigned long args[] = { fd, level, optname, (unsigned long) optval, optlen };
  return syscall (SYS_socketcall, SYS_SETSOCKOPT, args);
#endif
}

static inline int
real_getsockname (int fd, struct sockaddr *addr, socklen_t *len)
{
#ifdef SYS_getsockname
  return syscall (SYS_getsockname, fd, addr, len);
#else
  unsigned long args[] = { fd, (unsigned long) addr, (unsigned long) len };
  return syscall (SYS_socketcall, SYS_GETSOCKNAME, args);
#endif
}

static inline int
real_connect (int fd, const struct sockaddr *addr, socklen_t addr_len)
{
#ifdef SYS_connect
  return syscall (SYS_connect, fd, addr, addr_len);
#else
  unsigned long args[] = { fd, (unsigned long) addr, addr_len };
  return syscall (SYS_socketcall, SYS_CONNECT, args);
#endif
}

static inline ssize_t
real_read (int fd, void *buf, size_t n)
{
  return syscall (SYS_read, fd, buf, n);
}

static inline ssize_t
real_recvfrom (int fd, void *buf, size_t n, int flags, struct sockaddr *addr, socklen_t *addr_len)
{
#ifdef SYS_recvfrom
  return syscall (SYS_recvfrom, fd, buf, n, flags, addr, addr_len);
#else
  unsigned long args[] = { fd, (unsigned long) buf, n, flags, (unsigned long) addr, (unsigned long) addr_len };
  return syscall (SYS_socketcall, SYS_RECVFROM, args);
#endif
}

static inline ssize_t
real_recv (int fd, void *buf, size_t n, int flags)
{
  socklen_t addr_len = 0;
  return real_recvfrom (fd, buf, n, flags, NULL, &addr_len);
}

static inline ssize_t
real_write (int fd, const void *buf, size_t n)
{
  return syscall (SYS_write, fd, buf, n);
}

static inline int
real_bind (int fd, const struct sockaddr *addr, socklen_t addr_len)
{
#ifdef SYS_bind
  return syscall (SYS_bind, fd, addr, addr_len);
#else
  unsigned long args[] = { fd, (unsigned long) addr, addr_len };
  return syscall (SYS_socketcall, SYS_BIND, args);
#endif
}

static inline int
real_listen (int fd, int n)
{
#ifdef SYS_listen
  return syscall (SYS_listen, fd, n);
#else
  unsigned long args[] = { fd, n };
  return syscall (SYS_socketcall, SYS_LISTEN, args);
#endif
}

static inline int
real_accept (int listenfd, struct sockaddr *addr, socklen_t *addr_len)
{
#ifdef SYS_accept
  return syscall (SYS_accept, listenfd, addr, addr_len);
#else
  unsigned long args[] = { listenfd, (unsigned long) addr, (unsigned long) addr_len };
  return syscall (SYS_socketcall, SYS_ACCEPT, args);
#endif
}

static inline int
real_close (int fd)
{
  return syscall (SYS_close, fd);
}

#endif
